package com.yanghui.dtracing.reporter.zipkin;

import com.yanghui.dtracing.core.opentracing.DTracerSpan;
import com.yanghui.dtracing.core.opentracing.reporter.Reporter;
import zipkin2.Span;
import zipkin2.reporter.AsyncReporter;

public class ZipkinReporter implements Reporter {

    private ZipkinRestTemplateSender sender;

    private AsyncReporter<Span> delegate;

    private ZipkinV2SpanAdapter zipkinV2SpanAdapter;

    @Override
    public void init() {
        String host = System.getProperty("zipkin.host","");
        if("".equalsIgnoreCase(host)){
            return;
        }
        this.sender = new ZipkinRestTemplateSender(host);
        this.delegate = AsyncReporter.create(this.sender);
        this.zipkinV2SpanAdapter = new ZipkinV2SpanAdapter();
    }

    @Override
    public void report(DTracerSpan tracerSpan) {
        if(null == tracerSpan || null == this.delegate){
            return;
        }
        Span zipkinSpan = zipkinV2SpanAdapter.convertToZipkinSpan(tracerSpan);
        this.delegate.report(zipkinSpan);
    }

    @Override
    public void close() {
        //nothing to do
    }
}
